package com.matrix.hellokafka.hellowold;

import com.matrix.hellokafka.util.ConfigUtil;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;

public class HelloKafkaComsumer {

    public static void main(String[] args) {

        Properties properties = ConfigUtil.getComsumerProperties();

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        try {

            consumer.subscribe(Collections.singletonList("my-topict"));
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500));
                records.forEach(record -> {
                    System.out.println(String.format("topic:%s,分区:%s,偏移量：%s,key:%s,value:%s",
                            record.topic(),
                            record.partition(),
                            record.offset(),
                            record.key(),
                            record.value()
                    ));
                });
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            consumer.close();
        }


    }

}
